/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.thelostlands.shinycast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.plugin.java.JavaPlugin;
import net.thelostlands.shinycast.runnable.Mercury;
import org.bukkit.plugin.RegisteredServiceProvider;

/**
 *
 * @author fabio
 */
public class Message extends JavaPlugin {

    CmdHandler cmdExec;
    public static final String mainDirectory = "plugins" + File.separator + "Shinycast";
    public static File messageConfig = new File(mainDirectory + File.separator + "messageConfig.yml");
    public static Properties prop = new Properties();
    public Mercury messaggero = new Mercury(this);
    protected static final Logger log = Logger.getLogger("Minecraft");
    public static Permission perms;
    
    @Override
    public void onDisable() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void onEnable() {
        setupDependencies();
        cmdExec = new CmdHandler(this);
        getCommand("message").setExecutor(cmdExec);
        if (!messageConfig.exists()) {
            createConfigFile();
        }
        if(loadConfigFile()){
            messaggero.start();
        }
    }
    
    
    private boolean setupDependencies() {
        Collection<RegisteredServiceProvider<Permission>> allPerms = this.getServer().getServicesManager().getRegistrations(net.milkbowl.vault.permission.Permission.class);
        for (RegisteredServiceProvider<Permission> perm : allPerms) {
            Permission p = perm.getProvider();
            log.info(String.format("[%s] Trovato (Permission) %s", getDescription().getName(), p.getName()));
        }

        perms = this.getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class).getProvider();
        log.info(String.format("[%s] Uso %s", getDescription().getName(), perms.getName()));
        return (perms != null);
    }
    

    private void createConfigFile() {
        try {
            File dir = new File(mainDirectory);
            dir.mkdirs();
            getServer().getLogger().info("Provo a creare il file");
            messageConfig.createNewFile();
            getServer().getLogger().warning("File creato");
            FileOutputStream output = new FileOutputStream(messageConfig);
            prop.put("messaggio", "Benvenuti nel server");
            prop.put("is-on", "True");
            prop.put("tempo-in-secondi", "60");
            prop.put("prefisso", "[Server]");
            prop.store(output, "Modificate le opzioni a vostropiacimento");
            output.flush();
            output.close();
        } catch (IOException ex) {
            Logger.getLogger(Message.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private boolean loadConfigFile() {
        try {
            FileInputStream input = new FileInputStream(messageConfig);
            prop.load(input);
            String rawMessage = prop.getProperty("messaggio");
            messaggero.setMessage(rawMessage);
            boolean recurringMessage = Boolean.parseBoolean(prop.getProperty("is-on"));
            int timeDelay = Integer.parseInt(prop.getProperty("tempo-in-secondi"));
            
            
            messaggero.setSeconds(timeDelay);
            String prefisso = prop.getProperty("prefisso");
            messaggero.setPrefix(prefisso);
            input.close();
            return recurringMessage;
        } catch (Exception ex) {
            messaggero.setPrefix("[Server]");
            messaggero.setMessage("Impostare il messaggio del server");
            messaggero.setSeconds(60);
            saveConfigFile();
            return true;
        }
    }

    public void saveConfigFile() {
        try {
            FileOutputStream output = new FileOutputStream(messageConfig);
            prop.put("messaggio", messaggero.getMessage());
            prop.put("tempo-in-secondi", "" + messaggero.getSeconds());
            prop.put("prefisso", messaggero.getPrefix());
            prop.store(output, "Modificate le opzioni a vostro piacimento");
            output.flush();
            output.close();
        } catch (Exception ex) {
            Logger.getLogger(Message.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
